home *** CD-ROM | disk | FTP | other *** search
/ NOVA - For the NeXT Workstation / NOVA - For the NeXT Workstation.iso / Documents / NeXTAnswers / appkit.714 < prev    next >
Text File  |  1992-02-06  |  3KB  |  99 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f2\fswiss Helvetica;}
  2. \paperw12740
  3. \paperh8500
  4. \margl120
  5. \margr120
  6. {\colortbl\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ul0\fs28 Proper Use of  HashTable Objects\
  8.     \
  9. Q:  
  10. \fc0 How do I use the arguments to the various methods on HashTable ? I don't understand the mechanics of HashTable storage of my data.\
  11.  
  12. \i \
  13.  
  14. \i0 A:  The following code snippet shows how the arguments to the various methods on HashTable should be used.  Note that even though data can be stored in the hash table as 
  15. \b int
  16. \b0 , 
  17. \b char *
  18. \b0 , 
  19. \b  id
  20. \b0 ,  
  21. \b void *
  22. \b0 , or any other 32-bit quantity that can be described by a type string,  the arguments of the hash table methods, such as\
  23.  
  24. \b insertKey:value:
  25. \b0  , 
  26. \b nextState:key:value
  27. \b0 ,  
  28. \b valueForKey:
  29. \b0  have to be casted to void * or const void * respectively.  See the documentation on Hash Table for more details. \
  30. \
  31.  
  32. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f1\fs24\fc0 static char *month_name[]=\{\
  33.     "january", "february", "march",\
  34.     "april", "may", "june",\
  35.     "july", "august", "september",\
  36.     "october", "november", "december" \};\
  37.  
  38. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0 \
  39.  
  40. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f1\fs24\fc0 - hashTableTest:sender\
  41. \{\
  42.     NXHashState state;    \
  43.     const void *key;\
  44.     void *val;\
  45.     id table;\
  46.     \
  47.     unsigned count = 0;\
  48.     \
  49.     \
  50.     /* Allocate a hash table with the capacity of 12 entries. The Key\
  51.      * type is char *, the Value type is int.\
  52.      */\
  53.     \
  54.     table =[[HashTable alloc] initKeyDesc:"*" valueDesc:"i" capacity:12];\
  55.     \
  56.     /* Insert data into the table */\
  57.     while ( count < 12 )\
  58.         \{\
  59.         [table insertKey:(const void *)month_name[count] value:(void *)count];\
  60.         count++;\
  61.         \}\
  62.         \
  63.     /* Retrieve the key/value pairs from the table\
  64.      * and display them.\
  65.      * Note that you can only enumerate entries in the table once it has been\
  66.      * initialized with the insertKey:value: method.\
  67.      */\
  68.     state = [table initState];\
  69.     \
  70.     while ( [table nextState:&state key:(void *)&key value:(void *)&val] )\
  71.         \{\
  72.         fprintf(stderr, "key %s value %d\\n", key, val);\
  73.         \}\
  74.     \
  75.     /* Retrieve a particular value from the table\
  76.      * knowing its key\
  77.      */\
  78.     if ( [table isKey:(const void *)"may"] )    \
  79.         val = [table valueForKey: (void*)"may"];\
  80.         \
  81.         \
  82.         \
  83.     return self;\
  84. \}\
  85. \
  86.  
  87. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0 \
  88.  Valid for 1.0 \
  89.  ( The 2.0 method 
  90. \b initKeyDesc:valueDesc: capacity
  91. \b0  should be replaced by  the 1.0 method 
  92. \b newKeyDesc:valueDesc:capacity
  93. \b0 )\
  94. Valid for 2.0\
  95. \
  96. QA714\
  97. \
  98.  
  99.